
package com.dps.omc.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.enumer.EnumGroupType;
import com.dps.omc.enumer.EnumTeam;
import com.dps.omc.task.message.DesUtils;
import com.dps.omc.utils.jdbc.JdbcConnect;
import com.dps.omc.utils.jdbc.JdbcServiceManager;
import com.dps.omc.model.*;
import com.dps.omc.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class DbServerAction extends AbstractAdminController<IDbServerService>{

    @Autowired
    private IDbServerService dbServerService;
    @Autowired
    private IGroupTypeService groupTypeService;
    @Autowired
    private IBizTypeService bizTypeService;
    @Autowired
    private IAgentService agentService;
    @Autowired
    private IAgentNodeService agentNodeService;
    @Autowired
    private ISystemRankService systemRankService;
    @Autowired
    private IOsTypeService osTypeService;
    @Autowired
    private IDbTypeService dbTypeService;
    @Autowired
    private JdbcServiceManager jdbcServiceManager;
    
    @RequestMapping(value="/dbserver/list")
    public String list(DbServer dbServer, ModelMap model, PageParam pageParam, HttpServletRequest request, HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(dbServer); 	
    	BizData4Page<DbServer> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("dbServer", dbServer);
    	return "/module/dbserver/list";
    }    
    
    @RequestMapping(value="/dbserver/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

        List<GroupType> groupTypes = groupTypeService.findAll();
        List<BizType> bizTypes = bizTypeService.findAll();
        List<Agent> agents = agentService.findAll();
        List<SystemRank> systemRanks = systemRankService.findAll();
        List<OsType> osTypes = osTypeService.findAll();
        List<DbType> dbTypes = dbTypeService.findAll();

        model.addAttribute("groupTypes", groupTypes);
        model.addAttribute("bizTypes", bizTypes);
        model.addAttribute("agents", agents);
        model.addAttribute("systemRanks", systemRanks);
        model.addAttribute("osTypes", osTypes);
        model.addAttribute("dbTypes", dbTypes);
        model.addAttribute("teams", EnumTeam.values());

        model.addAttribute("dbServer", new DbServer());
        return "module/dbserver/edit";
    }    
    
    @RequestMapping(value="/dbserver/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	DbServer dbServer = dbServerService.fetch(id);
        List<GroupType> groupTypes = groupTypeService.findAll();
        List<BizType> bizTypes = bizTypeService.findAll();
        List<Agent> agents = agentService.findAll();
        List<SystemRank> systemRanks = systemRankService.findAll();
        List<OsType> osTypes = osTypeService.findAll();
        List<DbType> dbTypes = dbTypeService.findAll();

        model.addAttribute("groupTypes", groupTypes);
        model.addAttribute("bizTypes", bizTypes);
        model.addAttribute("agents", agents);
        model.addAttribute("systemRanks", systemRanks);
        model.addAttribute("osTypes", osTypes);
        model.addAttribute("dbTypes", dbTypes);
        model.addAttribute("teams", EnumTeam.values());
    	model.addAttribute("dbServer", dbServer);
        return "module/dbserver/edit";
    }    
    
    @RequestMapping(value="/dbserver/save")
    @ResponseBody
    public String save(HttpServletRequest request,DbServer dbServer){
		boolean isAddNew = StringUtils.isBlank(dbServer.getId())?true:false;
		if(isAddNew)
		{
			dbServer.setId(IdGenerator.createNewId());
            dbServer.setGroupType(EnumGroupType.DBSERVER);
			dbServerService.add(dbServer);
		}
		else
		{
            dbServer.setGroupType(EnumGroupType.DBSERVER);
			dbServerService.update(dbServer);
		}
        Agent agent = agentService.fetch(dbServer.getAgentId());

        agentNodeService.createByDbServer(agent,dbServer);
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/dbserver/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	dbServerService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }

    @RequestMapping(value="/dbserver/testconnect")
    @ResponseBody
    public String testConnect(HttpServletRequest request) throws Exception {

        String dbHost = request.getParameter("dbHost");
        String dbType = request.getParameter("dbType");
        String dbName = request.getParameter("dbName");
        String dbUser = request.getParameter("dbUser");
        String dbPass = request.getParameter("dbPass");
        dbPass = DesUtils.decrypt(dbPass);
        String dbPort = request.getParameter("dbPort");


//        System.out.println(dbHost);
//        System.out.println(dbType);
//        System.out.println(dbName);
//        System.out.println(dbUser);
//        System.out.println(dbPass);
//        System.out.println(dbPort);

        JdbcConnect jdbcConnect = new JdbcConnect();
        jdbcConnect.setDbHost(dbHost);
        jdbcConnect.setDbName(dbName);
        jdbcConnect.setDbPort(dbPort);
        jdbcConnect.setDbType(dbType);
        jdbcConnect.setDbUser(dbUser);
        jdbcConnect.setDbPass(dbPass);

        return jdbcServiceManager.testConnect(jdbcConnect);
//        boolean res = jdbcServiceManager.testConnect(jdbcConnect);
//        if(res)
//        {
//            return "true";
//        }
//        else
//        {
//            return "false";
//        }
    }

    protected Map getQueryMap(DbServer dbServer)
    {
        Map<String, Object> conditions = new HashMap();

        conditions.put("agentId", dbServer.getAgentId());
        conditions.put("alias", dbServer.getAlias());
        conditions.put("dbType", dbServer.getDbType());
        conditions.put("dbName", dbServer.getDbName());
        conditions.put("dbUser", dbServer.getDbUser());
        conditions.put("dbPass", dbServer.getDbPass());
        conditions.put("dbPort", dbServer.getDbPort());
        conditions.put("dbHost", dbServer.getDbHost());
        conditions.put("bizTypeId", dbServer.getBizTypeId());
        conditions.put("bizTypeName", dbServer.getBizTypeName());
        conditions.put("sysRank", dbServer.getSysRank());
        conditions.put("opSystem", dbServer.getOpSystem());
        conditions.put("note", dbServer.getNote());
        conditions.put("otherParam", dbServer.getOtherParam());
        conditions.put("groupType", dbServer.getGroupType());
        conditions.put("createDate", dbServer.getCreateDate());
        return conditions;
    }

    @Override
    protected IDbServerService getMainService() {
        return dbServerService;
    }

    @Override
    protected String getMainObjName() {
        return "dbserver";
    }

    @Override
    protected String getViewTitle() {
        return "dbserver";
    }
}
